{include file="pageheader_erp.htm"}

<link href="styles/erp_delivery.css" rel="stylesheet" type="text/css" />

{insert_scripts files="../js/jquery.1.3.2.js"}
{insert_scripts files="../js/jquery.blockUI.2.23.js"}


{if $act eq 'edit'}
{if $delivery_info.available_act.edit eq 1}
<script type=text/javascript>
	
	function edit_delivery()
	{
		$("#select_warehouse").show();
		$("#span_warehouse").hide();		
		$("#select_delivery_style").show();
		$("#span_delivery_style").hide();	
		$("#span_delivery_to").hide();
		$("#input_delivery_to").show();							
	}


	function post_delivery(delivery_id)
	{
		function postDeliveryResponse(res)
		{
			if(res.error!=0)
			{
				alert(res.message);
			}
			else{
				window.document.location="./erp_warehouse_manage.php?act=delivery_list";
			}				
		}
		if(confirm('{$lang.erp_delivery_sure_to_post_delivery}'))
		{	
			Ajax.call('erp_delivery.php?act=post_delivery', 'delivery_id='+delivery_id, postDeliveryResponse, 'POST', 'JSON');
		}
	}	
		
	function delete_delivery(delivery_id)
	{
		function deletedeliveryResponse(res)
		{
			if(res.error!=0)
			{
				alert(res.message);
			}
			else{
				window.document.location="erp_warehouse_manage.php?act=delivery_list";;
			}			
		}
		if(confirm('{$lang.erp_delivery_sure_to_delete_delivery}'))
		{	
			Ajax.call('erp_delivery.php?act=delete_delivery', 'delivery_id='+delivery_id, deletedeliveryResponse, 'POST', 'JSON');
		}			
	}
	
	function edit_delivery_item(delivery_item_id)
	{
		$("#span_delivered_qty_"+delivery_item_id).hide();
		$("#input_delivered_qty_"+delivery_item_id).show().focus();
	}
	
	function delete_delivery_item(delivery_item_id)
	{
		var delivery_id='{$delivery_info.delivery_id}';
		
		function deletedeliveryItemResponse(res)
		{
			if(res.error!=0)
			{
				alert(res.message);
			}
			else{
				window.document.location="erp_warehouse_manage.php?act=edit_delivery&id="+delivery_id;
			}			
		}
		
		if(confirm('{$lang.erp_delivery_sure_to_delete_delivery_item}'))
		{	
			Ajax.call('erp_delivery.php?act=delete_delivery_item', 'delivery_id='+delivery_id+'&delivery_item_id='+delivery_item_id, deletedeliveryItemResponse, 'POST', 'JSON');
		}		
	}	

	
	$(document).ready(function(){
		
		$("#span_warehouse").css('cursor','pointer');
		$("#span_warehouse").mouseover(function(){
			$(this).css('color','red');
		});
		$("#span_warehouse").mouseout(function(){
			$(this).css('color','black');
		});	
		$("#span_warehouse").click(function(){
			$(this).hide();
			$("#select_warehouse").show().focus();
		});
		$("#select_warehouse").change(function(){
			var warehouse_id=$(this).attr('value');
			var delivery_id='{$delivery_info.delivery_id}';
			
			if(warehouse_id==0)
			{
				return;
			}
			
			function changeWarehouseResponse(res)
			{
				if(res.error!=0)
				{
					alert(res.message);
				}
				else{
					window.document.location="erp_warehouse_manage.php?act=edit_delivery&id="+delivery_id;
				}
			}
			
			if(confirm('{$lang.erp_delivery_sure_to_change_warehouse}'))
			{
					Ajax.call('erp_delivery.php?act=change_warehouse', 'delivery_id='+delivery_id+'&warehouse_id='+warehouse_id, changeWarehouseResponse, 'POST', 'JSON');
			}
			else{
				$("#select_warehouse").hide();
				$("#span_warehouse").show();
			}
		});		
		
		$("#span_delivery_style").css('cursor','pointer');
		$("#span_delivery_style").mouseover(function(){
			$(this).css('color','red');
		});
		$("#span_delivery_style").mouseout(function(){
			$(this).css('color','black');
		});	
		$("#span_delivery_style").click(function(){
			$(this).hide();
			$("#select_delivery_style").show().focus();
		});
		$("#select_delivery_style").change(function(){
			var delivery_style_id=$(this).attr('value');
			var delivery_id='{$delivery_info.delivery_id}';
			
			function changedeliveryStyleResponse(res)
			{
				if(res.error!=0)
				{
					alert(res.message);
				}
				else{
					window.document.location="erp_warehouse_manage.php?act=edit_delivery&id="+delivery_id;
				}
			}
			
			if(confirm('{$lang.erp_delivery_sure_to_change_delivery_style}'))
			{
					Ajax.call('erp_delivery.php?act=change_delivery_style', 'delivery_id='+delivery_id+'&delivery_style_id='+delivery_style_id, changedeliveryStyleResponse, 'POST', 'JSON');
			}
			else{
				$("#select_delivery_style").hide();
				$("#span_delivery_style").show();				
			}
		});			
					
		$("#span_order").css('cursor','pointer');
		$("#span_order").mouseover(function(){
			$(this).css('color','red');
		});
		$("#span_order").mouseout(function(){
			$(this).css('color','black');
		});	
		$("#span_order").click(function(){
			$(this).hide();
			$("#select_order").show().focus();
		});
		$("#select_order").change(function(){
			var order_id=$(this).attr('value');
			var delivery_id='{$delivery_info.delivery_id}';
			
			function changeOrderResponse(res)
			{
				if(res.error!=0)
				{
					alert(res.message);
				}
				else{
					window.document.location="erp_warehouse_manage.php?act=edit_delivery&id="+delivery_id;
				}
			}
			if(order_id!='')
			{
			if(confirm('{$lang.erp_delivery_sure_to_change_order}'))
			{
					Ajax.call('erp_delivery.php?act=change_order', 'delivery_id='+delivery_id+'&order_id='+order_id, changeOrderResponse, 'POST', 'JSON');
			}
			else{
				$("#select_order").hide();
				$("#span_order").show();				
			}
			}
		});

		$("#span_delivery_to").css('cursor','pointer');
		$("#span_delivery_to").mouseover(function(){
			$(this).css('color','red');
		});
		$("#span_delivery_to").mouseout(function(){
			$(this).css('color','black');
		});	
		$("#span_delivery_to").click(function(){
			$(this).hide();
			$("#input_delivery_to").show().focus();
		});	
		$("#input_delivery_to").change(function(){
			var delivery_to=$(this).attr('value');
			var delivery_id='{$delivery_info.delivery_id}';
			
			function changedeliveryToResponse(res)
			{
				if(res.error!=0)
				{
					alert(res.message);
				}
				else{
					window.document.location="erp_warehouse_manage.php?act=edit_delivery&id="+delivery_id;
				}
			}
			
			if(delivery_to=='')
			{
				alert('{$lang.erp_delivery_delivery_to_required}');
			}
			else{
				
			if(confirm('{$lang.erp_delivery_sure_to_change_delivery_to}'))
			{
					Ajax.call('erp_delivery.php?act=change_delivery_to', 'delivery_id='+delivery_id+'&delivery_to='+delivery_to, changedeliveryToResponse, 'POST', 'JSON');
			}
			else{
				$("#span_delivery_to").hide();
				$("#input_delivery_to").show();				
			}
			}
		});
		$("#input_delivery_to").blur(function(){
				$("#span_delivery_to").show();
				$("#input_delivery_to").hide();				
		});
		
		$("span[id^='span_delivered_qty_']").css('cursor','pointer');
		$("span[id^='span_delivered_qty_']").mouseover(function(){
			$(this).css('color','red');
		});
		$("span[id^='span_delivered_qty_']").mouseout(function(){
			$(this).css('color','black');
		});	
		$("span[id^='span_delivered_qty_']").click(function(){
			var delivery_item_id=$(this).attr('id');
			delivery_item_id=delivery_item_id.substr('span_delivered_qty_'.length,delivery_item_id.length-'span_delivered_qty_'.length);			
			
			$(this).hide();
			$("#input_delivered_qty_"+delivery_item_id).show().focus();
		});	
		
		$("input[id^='input_delivered_qty_']").change(function(){
			var delivery_item_id=$(this).attr('id');
			delivery_item_id=delivery_item_id.substr('input_delivered_qty_'.length,delivery_item_id.length-'input_delivered_qty_'.length);
			
			var delivery_id='{$delivery_info.delivery_id}';
			var delivered_qty=$(this).attr('value');
			
			function changeDeliveredQtyResponse(res)
			{
				if(res.error!=0)
				{
					alert(res.message);
					
					$("#span_delivered_qty_"+delivery_item_id).show();	
					$("#input_delivered_qty_"+delivery_item_id).attr('value',$("#span_delivered_qty_"+delivery_item_id).text()).hide();					
				}
				else{
					$("#span_delivered_qty_"+delivery_item_id).text(delivered_qty);
					$("#input_delivered_qty_"+delivery_item_id).hide();
					$("#span_delivered_qty_"+delivery_item_id).show();
				}
			}
			
			if(isNaN(delivered_qty))
			{
				alert('{$lang.erp_not_a_number}');
			}
			else{
				if(confirm('{$lang.erp_delivery_sure_to_change_delivered_qty}'))
				{
					Ajax.call('erp_delivery.php?act=change_delivered_qty', 'delivery_id='+delivery_id+'&delivery_item_id='+delivery_item_id+"&delivered_qty="+delivered_qty, changeDeliveredQtyResponse, 'POST', 'JSON');
				}
				else{
					$("#span_delivered_qty_"+delivery_item_id).show();	
					$("#input_delivered_qty_"+delivery_item_id).attr('value',$("#span_delivered_qty_"+delivery_item_id).text()).hide();
				}
			}
		});
		$("input[id^='input_delivered_qty_']").blur(function(){
			var delivery_item_id=$(this).attr('id');
			delivery_item_id=delivery_item_id.substr('input_delivered_qty_'.length,delivery_item_id.length-'input_delivered_qty_'.length);			
			$("#span_delivered_qty_"+delivery_item_id).show();
			
			$(this).hide();
		});
		
		
		$("#input_goods_sn").focus();
		
		$("#input_goods_sn").change(function(){
			var goods_sn=$(this).attr('value');
	
			function checkGoodsSnResponse(res)
			{
				if(res.error!=0)
				{
					alert(res.message);
					$("#input_goods_sn").focus();
					$("#input_goods_sn").attr('value','');
				}
				else{
					$("#attr_box").html(res.attr_info);
					$("#input_qty").focus();
				}
			}
			if(goods_sn=='')
			{
				alert('{$lang.erp_delivery_goods_sn_required}');
			}
			else{
				Ajax.call('erp_delivery.php?act=check_goods_sn', 'goods_sn='+goods_sn, checkGoodsSnResponse, 'POST', 'JSON');
			}
		});

		
		$("#input_qty").keyup(function(){
			var input_qty=$(this).attr('value');
			var last_char=input_qty.substr(input_qty.length-1,1);
			if(isNaN(last_char))
			{
				$(this).attr('value',input_qty.substr(0,input_qty.length-1));
			}
		});	
			
		$("#img_submit_delivery").click(function(){
			
			var goods_sn=$("#input_goods_sn").attr('value');
			
			var goods_qty=$("#input_qty").attr('value');
			
			var delivery_id='{$delivery_info.delivery_id}';
			
			var ids='';
			
			$("select[id^='select_attr_']").each(function(index){
				
				var value=$($("select[id^='select_attr_']")[index]).attr('value');
				
				ids=ids+value+',';
				
				
			});
			
			function updatedeliveryResponse(res)
			{
				if(res.error!=0)
				{
					alert(res.message);
				}
				else{
					window.document.location="erp_warehouse_manage.php?act=edit_delivery&id="+delivery_id;
				}
			}
			if(goods_sn=='')
			{
				alert('{$lang.erp_warehousing_goods_sn_required}');	
			}
			else{
				Ajax.call('erp_delivery.php?act=update_delivery', 'delivery_id='+delivery_id+'&goods_sn='+goods_sn+'&ids='+ids+'&goods_qty='+goods_qty, updatedeliveryResponse, 'POST', 'JSON');
			}			
		});
		
	});
	
	
</script>
{/if}
{/if}


{if $delivery_info.available_act.approve eq 1}

<!-- start approve remark -->

<div class="approve_remark" id="approve_remark">
<div class="approve_remark_head">
	<table cellpadding="0" cellspacing="0" border='0'>
		<tr>
			<td>
				<img src="./images/approve_remark.gif" />
			</td>
			<td align="right">
				<span><img style="cursor:pointer;" id="img_close_approve_remark" src="./images/close_erp.gif" /></span>
			</td>
		</tr>
	</table>
</div>

<div class="approve_remark_box">
<table  cellpadding="5" cellspacing="0" border='0'>
	 <tr>
    	<td width="60">{$lang.erp_approve_remark}</td>	
		<td>
			<textarea rows="8" cols="55" id="textarea_approve_remark">{$delivery_info.approve_remark}</textarea>
		</td>
	</tr>	
</table>
</div>
<div class="div_submit_approve">
	<img style="cursor:pointer;" id="img_submit_approve" src="./images/submit.gif" />
</div>	
</div>
 
<!-- end approve remark -->

<script type=text/javascript>

$(document).ready(function(){
		$("#img_close_approve_remark").click(function(){
			$.unblockUI();
		});
});
	
function approve_delivery(delivery_id,act)
{
		function approvedeliveryResponse(res)
		{
			if(res.error!=0)
			{
				alert(res.message);
			}
			else{
				window.document.location="./erp_warehouse_manage.php?act=delivery_list";
			}
		}
		
		if(act=='pass')
		{
			$("#img_submit_approve").bind('click',function(){
			var approve_remark=$("#textarea_approve_remark").attr('value');
			if(confirm('{$lang.erp_delivery_sure_to_post_approve}'))
			{
				Ajax.call('erp_delivery.php?act=approve_pass', 'delivery_id='+delivery_id+'&approve_remark='+approve_remark, approvedeliveryResponse, 'POST', 'JSON');
			}
			else{
				$.unblockUI();
				$(this).unbind('click');			
			}					
			});

			$.blockUI({
		   	message: $('#approve_remark'), css: { 
		    top:  ($(window).height()-200) /2 + 'px', 
		    left: ($(window).width()-500) /2 + 'px'
		   }
		});	
		}
		else if(act=='reject')
		{
			$("#img_submit_approve").bind('click',function(){
			var approve_remark=$("#textarea_approve_remark").attr('value');
			if(confirm('{$lang.erp_delivery_sure_to_post_approve}'))
			{
				Ajax.call('erp_delivery.php?act=approve_reject', 'delivery_id='+delivery_id+'&approve_remark='+approve_remark, approvedeliveryResponse, 'POST', 'JSON');
			}
			else{
				$.unblockUI();
				$(this).unbind('click');			
			}					
			});
			$.blockUI({
		   	message: $('#approve_remark'), css: { 
		    top:  ($(window).height()-200) /2 + 'px', 
		    left: ($(window).width()-500) /2 + 'px'
		   	}			
			});
		}
	}
	
</script>		
{/if}

<!-- start delivery info -->
 <div class="list-div" id="listDiv">
<table  cellpadding="3" cellspacing="1">
  <tr>
    <th>{$lang.erp_delivery_sn}</th>	
	<th width="15%">{$lang.erp_delivery_warehouse}</th>
	<th>{$lang.erp_delivery_admin}</th>		
 	<th width="15%">{$lang.erp_delivery_delivery_style}</th>	
	<th width="10%">{$lang.erp_delivery_order}</th>	
	<th width="10%">{$lang.erp_delivery_to}</th>
	<th width="10%">{$lang.erp_delivery_date}</th>
	{if $act neq 'view'}
	<th width="15%">{$lang.erp_operation}</th>
	{/if}
  </tr>
  
 	<tr>
		<td align="center">{$delivery_info.delivery_sn}</td>
		<td align="center">
			<span id="span_warehouse">{$delivery_info.warehouse_info.name}</span>
			{if $warehouse_info}
			<select name="select_warehouse" id="select_warehouse" style="display:none;">
				<option value="0" >{$lang.erp_delivery_select_warehouse}</option>
				{foreach from=$warehouse_info item=warehouse_info}
					<option value="{$warehouse_info.warehouse_id}" {if $warehouse_info.warehouse_id eq $delivery_info.warehouse_id}selected="selected"{/if}>{$warehouse_info.name}</option>
				{/foreach}
			</select>
			{/if}
		</td>
		<td align="center">{$delivery_info.admin_name}</td>
		<td align="center">
			<span id="span_delivery_style">{$delivery_info.delivery_style_info.delivery_style}</span>
			{if $delivery_style_info}
			<select name="select_delivery_style" id="select_delivery_style" style="display:none;">
				{foreach from=$delivery_style_info item=delivery_style_info}
					<option value="{$delivery_style_info.delivery_style_id}" {if $delivery_style_info.delivery_style_id eq $delivery_info.delivery_style_id}selected="selected"{/if}>{$delivery_style_info.delivery_style}</option>
				{/foreach}
			</select>
			{/if}			
			
		</td>
		<td align="center">
			
			<span id="span_order">
				{if $delivery_info.order_info.order_sn}
					{$delivery_info.order_info.order_sn}
				{else}
					{if $order_info}
						{$lang.erp_delivery_choose_order}
					{else}
						&nbsp;
					{/if}
				{/if}
			</span>
			
			{if $order_info}
			<select name="select_order" id="select_order" style="display:none;">
				<option value="">{$lang.erp_delivery_choose_order}</option>
				{foreach from=$order_info item=order_info}
					<option value="{$order_info.order_id}" {if $order_info.order_id eq $delivery_info.order_id}selected="selected"{/if}>{$order_info.order_sn}</option>
				{/foreach}
			</select>
			{/if}		
		</td>
		<td align="center">
			<span id="span_delivery_to">
				{if $delivery_info.delivery_to}
					{$delivery_info.delivery_to}
				{else}
					{$lang.erp_delivery_input_delivery_to}
				{/if}
			</span>
			<input id="input_delivery_to" size="10" maxlength="10" style="display:none;" value="{$delivery_info.delivery_to}"/>
		</td>
		<td align="center">{$delivery_info.delivery_date}</td>
		{if $act neq 'view'}
		<td align="center">
			
			{if $delivery_info.available_act.edit eq 1}
				<a href="javascript:edit_delivery()">{$lang.erp_operation_edit}</a>
			{/if}
			{if $delivery_info.available_act.delete eq 1}
				<a href="javascript:delete_delivery({$delivery_info.delivery_id})">{$lang.erp_operation_delete}</a>
			{/if}
	
			{if $delivery_info.available_act.post_to_approve eq 1}
				<a href="javascript:post_delivery({$delivery_info.delivery_id})">{$lang.erp_operation_post_to_approve}</a>
			{/if}				
			{if $delivery_info.available_act.approve eq 1}
				<a href="javascript: approve_delivery({$delivery_info.delivery_id},'pass')">{$lang.erp_operation_approve_pass}</a>
				<a href="javascript: approve_delivery({$delivery_info.delivery_id},'reject')">{$lang.erp_operation_approve_reject}</a>
			{/if}
		</td>
		{/if}
 	</tr>

</table>
</div>
<!-- end delivery info -->

<br/>


{if $act eq 'edit'}
{if $delivery_info.available_act.edit eq 1}
{if $delivery_info.delivery_style_id neq 1}
<div style="width:100%;margin-top:5px;margin-bottom:5px;text-align:left; border:1px solid #BBDDE5;background-color:#F4FAFB;">
	<table width="100%" cellpadding="5" cellspacing="0" border='0'>
		<tr>
			<td>
			<table cellpadding="0" cellspacing="0" >
			<tr>
			<td>
				<span style="margin-left:5px;margin-right:5px;">{$lang.erp_delivery_input_goods_sn}</span>
			</td>
			<td>
				<input size="15" maxlength="30" id="input_goods_sn" value="" />
			</td>
			
			<td>
				<span id="attr_box">
					
				</span>
			</td>
			
			<td>
				<span style="margin-left:10px;margin-right:5px;">{$lang.erp_delivery_input_qty}</span>
			</td>
			<td>
				<input id="input_qty" value="" size="5" maxlength="5" />
			</td>
			<td>
				<span style="margin-left:10px;"><input type="image"  id="img_submit_delivery" src="./images/submit.gif" style="cursor:pointer;" /></span>
			</td>
			</tr>
			</table>				
			</td>

		</tr>
	</table>		
</div>
{/if}
{/if}
{/if}

<!-- start delivery item info -->
 <div class="list-div" id="listDiv">
<table  cellpadding="3" cellspacing="1">
  <tr>
    <th>{$lang.erp_delivery_item_id}</th>	
	<th width="10%">{$lang.erp_delivery_item_goods_img}</th>
	<th width="10%">{$lang.erp_delivery_item_goods_sn}</th>		

	<th>{$lang.erp_delivery_item_goods_attr}</th>	
	<th>{$lang.erp_delivery_item_expected_qty}</th>
	<th>{$lang.erp_delivery_item_delivered_qty}</th>
	
	{if $price_visible eq 'true'}
		<th>{$lang.erp_delivery_item_delivery_price}</th>
	{/if}
	
	{if $act neq 'view' and $act neq 'approve'}
		<th width="10%">{$lang.erp_operation}</th>
	{/if}
  </tr>
  	
	{foreach from=$delivery_item_info key=key item=delivery_item}
 	<tr id="tr_{$delivery_item.goods_info.goods_barcode}">
		<td align="center">{$key}</td>
		<td align="center">
			<img {if $delivery_item.goods_details.goods_thumb}id="goods_img_{$delivery_item.delivery_item_id}"{/if} width="100" height="100" src="{if $delivery_item.goods_details.goods_thumb}{$delivery_item.goods_details.goods_thumb}{else}./images/no_img.gif{/if}">
		</td>
		<td align="center">{$delivery_item.goods_details.goods_sn}</td>

		<td align="center">
			
			{if $delivery_item.selected_attr}
			<table style="width:100px;">
				{foreach from=$delivery_item.selected_attr item=attr}
					<tr>
						<td>{$attr.attr_name}</td>
						<td>{$attr.attr_value}</td>
					</tr>
				{/foreach}
			</table>
			{else}
				$$	
			{/if}	
			
		</td>
		<td align="center">{$delivery_item.expected_qty}</td>
		<td align="center">
			<span id="span_delivered_qty_{$delivery_item.delivery_item_id}">{$delivery_item.delivered_qty}</span>
			<input size=5 maxlength="5" style="display:none;" id="input_delivered_qty_{$delivery_item.delivery_item_id}" value="{$delivery_item.delivered_qty}" />
		</td>
		
		{if $price_visible eq 'true'}
		<td align="center">
			<span id="span_delivery_price_{$delivery_item.delivery_item_id}">{$delivery_item.price}</span>
			<input size="8" maxlength="8" style="display:none;" id="input_delivery_price_{$delivery_item.delivery_item_id}" value="{$delivery_item.price}" />
		</td>		
		{/if}
		
		{if $act neq 'view' and $act neq 'approve'}
		<td align="center">
			
			{if $delivery_info.available_act.edit eq 1}
				<a href="javascript: edit_delivery_item({$delivery_item.delivery_item_id})">{$lang.erp_operation_edit}</a>
			{/if}
			{if $delivery_info.available_act.delete eq 1}
				<a href="javascript:delete_delivery_item({$delivery_item.delivery_item_id})">{$lang.erp_operation_delete}</a>
			{/if}				
		</td>
		{/if}
 	</tr>
	{/foreach}
</table>
</div>
<!-- end delivery item info -->



{include file="pagefooter.htm"}

